package com.xsherl.leetcode.solution;

import cn.hutool.core.lang.Assert;

import java.util.Arrays;

public class RemoveDuplicatesFromSortedArray {

    /**
     * 双指针，一个为遍历数组的指针，另一个为没有重复元素子数组的下标
     */
    public int removeDuplicates(int[] nums) {
        int m = 0;
        for (int i = 1; i < nums.length; ++i){
            if (nums[i] != nums[m]){
                m++;
                int t = nums[i];
                nums[i] = nums[m];
                nums[m] = t;
            }
        }
        return m + 1;
    }

    public static void main(String[] args) {
        int[] nums = {0,2};
        int i = new RemoveDuplicatesFromSortedArray().removeDuplicates(nums);
        System.out.println(Arrays.toString(nums));
        Assert.isTrue(i == 2);
    }

}
